// 图形库
#include <graphics.h>
// 标准库
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

#define PI 3.14159265358979323846
using namespace std;


void rotate_point(int cx, int cy, int x, int y, float angle, int* outX, int* outY)// 旋转坐标计算函数
{
	// 将点平移到原点
	x -= cx;
	y -= cy;	
	// 计算旋转后的坐标
	*outX = cx + (int)(x * cos(angle) - y * sin(angle));
	*outY = cy + (int)(x * sin(angle) + y * cos(angle));
}



void drawPointer(int cx, int cy, int radius, float angle)// 绘制旋转指针
{
	// 计算比例因子 (基于原始设计为150半径的表盘)
	const float scale = radius / 150.0f;	
	// 指针各部分尺寸定义
	float pointer_width = 25.0f * scale;    // 指针宽度
	const float pointer_length = 125.0f * scale;  // 指针总长度
	const float pointer_tip_length = 40.0f * scale;  // 指针尖端超出主体的长度	
	// 指针各部分坐标（比例化后）
	// 指针主体矩形坐标
	int rect_left = cx - static_cast<int>(pointer_width/2);
	int rect_top = cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20;
	int rect_right = cx + static_cast<int>(pointer_width/2);
	int rect_bottom = cy - static_cast<int>(pointer_tip_length) + radius / 20+7.0f * scale;
	// 指针三角形坐标
	int tri_points[3][2] = 
	{
		{cx - static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1},
		{cx + static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1} ,
		{cx, cy - static_cast<int>(pointer_length) + radius / 20+1+7.0f * scale} 
	};
	
	pointer_width = 7.0f * scale;    // 指针宽度
	// 指针底部矩形坐标
	int bottom_rect_left = cx - static_cast<int>(pointer_width/2);
	int bottom_rect_top = cy;
	int bottom_rect_right = cx + static_cast<int>(pointer_width/2);
	int bottom_rect_bottom = cy - static_cast<int>(pointer_tip_length);
	
//	// 指针尾部坐标
//	int tail_rect_left = cx - static_cast<int>(pointer_width/2);
//	int tail_rect_top = cy;
//	int tail_rect_right = cx + static_cast<int>(pointer_width/2);
//	int tail_rect_bottom = cy + static_cast<int>(pointer_tail_length);
	
	// 旋转后的坐标
	int rotated_rect[4][2];
	int rotated_tri[3][2];
	int rotated_bottom_rect[4][2];

	
	// 计算旋转后的矩形坐标
	rotate_point(cx, cy, rect_left, rect_top, angle, &rotated_rect[0][0], &rotated_rect[0][1]);
	rotate_point(cx, cy, rect_right, rect_top, angle, &rotated_rect[1][0], &rotated_rect[1][1]);
	rotate_point(cx, cy, rect_right, rect_bottom, angle, &rotated_rect[2][0], &rotated_rect[2][1]);
	rotate_point(cx, cy, rect_left, rect_bottom, angle, &rotated_rect[3][0], &rotated_rect[3][1]);
	
	// 计算旋转后的三角形坐标
	for (int i = 0; i < 3; i++)
	{
		rotate_point(cx, cy, tri_points[i][0], tri_points[i][1], angle, 
			&rotated_tri[i][0], &rotated_tri[i][1]);
	}
	
	// 绘制指针底部（深色部分）
	setfillcolor(EGERGB(17,14,11));
	setcolor(EGERGB(17,14,11));
	
	// 计算并绘制底部矩形
	rotate_point(cx, cy, bottom_rect_left, bottom_rect_top, angle, 
		&rotated_bottom_rect[0][0], &rotated_bottom_rect[0][1]);
	rotate_point(cx, cy, bottom_rect_right, bottom_rect_top, angle, 
		&rotated_bottom_rect[1][0], &rotated_bottom_rect[1][1]);
	rotate_point(cx, cy, bottom_rect_right, bottom_rect_bottom, angle, 
		&rotated_bottom_rect[2][0], &rotated_bottom_rect[2][1]);
	rotate_point(cx, cy, bottom_rect_left, bottom_rect_bottom, angle, 
		&rotated_bottom_rect[3][0], &rotated_bottom_rect[3][1]);
	
	ege_point bottom_points[4];
	for (int i = 0; i < 4; i++)
	{
		bottom_points[i].x = rotated_bottom_rect[i][0];
		bottom_points[i].y = rotated_bottom_rect[i][1];
	}
	ege_fillpoly(4, bottom_points);
	
	
	
// 绘制指针底部（深色部分）
	const float scale1 = radius / 240.0f;
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  30 * scale1);  
	
	
	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,               
			90 + i*2,10,NULL);
	}
	
	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(
			cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,
			260 -i*2,10,NULL);
	}
	
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  40 * scale1);
	
	
	// 绘制指针主体（白色部分）
	setfillcolor(WHITE);
	setcolor(WHITE);
	ege_point rect_points[4];
	for (int i = 0; i < 4; i++)
	{
		rect_points[i].x = rotated_rect[i][0];
		rect_points[i].y = rotated_rect[i][1];
	}
	ege_fillpoly(4, rect_points);
	
	// 绘制指针尖端（三角形）
	ege_point tri_points_ege[3];
	for (int i = 0; i < 3; i++)
	{
		tri_points_ege[i].x = rotated_tri[i][0];
		tri_points_ege[i].y = rotated_tri[i][1];
	}
	ege_fillpoly(3, tri_points_ege);
}



void drawScaleNum(int centerX, int centerY, int radius)
{
	// 预计算数字位置
	int numPos[5][2] = {0};
	for (int i = 0; i < 5; i++) 
	{
		double angle = -i * 60;
		double rad = angle * PI / 180;
		numPos[i][0] = centerX + (radius - radius*0.25) * cos(rad) - radius*0.053;
		numPos[i][1] = centerY - (radius - radius*0.25) * sin(rad) - radius*0.053;
	}
	
		setcolor(WHITE);
	// 绘制刻度
	for (int i = 0; i <= 40; i++) 
	{
		double angle =  -i * 6;
		double rad = angle * PI / 180;
		int len = 0;
		if(i % 10 == 0)
		{
			len = radius *0.15;
			setlinestyle(SOLID_LINE, 0, radius/100);
			ege_line(
				centerX + (radius - len) * cos(rad),
				centerY - (radius - len) * sin(rad),
				centerX + radius * cos(rad),
				centerY - radius * sin(rad));
		}
		else
		{
			len = radius*0.05;
			setlinestyle(SOLID_LINE, 0, radius/200);
			ege_line(
				centerX + (radius - len - radius*0.1) * cos(rad),
				centerY - (radius - len - radius*0.1) * sin(rad),
				centerX + (radius - radius*0.1) * cos(rad),
				centerY - (radius - radius*0.1) * sin(rad));
		}
	}	
	
	// 绘制数字
	setbkmode(TRANSPARENT);  // 设置文字背景透明	
	setcolor(WHITE);
	setfont(radius*0.14, 0, "SimHei");
	for (int i = 0; i <= 4; i++)
	{
		char str[3];
		sprintf(str, "%d", i);
		xyprintf(numPos[i][0], numPos[i][1], "%s", str);
	}	
	
	//书写文字
	setcolor(WHITE);
	LOGFONTW lf1;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf1, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf1.lfHeight = radius *0.12;           // 字体高度为  像素
	lf1.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf1.lfItalic = FALSE;        // 非斜体
	lf1.lfUnderline = FALSE;     // 无下划线
	lf1.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf1.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf1);	// 设置字体
	ege_outtextxy((int) centerX-radius*0.14, (int)centerY-radius*0.65,"HYD");
	ege_outtextxy((int) centerX-radius*0.4, (int)centerY-radius*0.55,"BRAKE  PRESS");
	setcolor(EGERGB(50,130,246));
	LOGFONTW lf2;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf2, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf2.lfHeight = radius *0.08;           // 字体高度为  像素
	lf2.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf2.lfItalic = FALSE;        // 非斜体
	lf2.lfUnderline = FALSE;     // 无下划线
	lf2.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf2.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf2);	// 设置字体
	ege_outtextxy((int) centerX-radius*0.25, (int)centerY+radius*0.5,"PSI × 1000");
	
}


void drawHYD(float centerX, float centerY, float radius, float angle)	//绘制液压刹车压力（HYD BRAKE PRESS ）仪表
{
	setcolor(EGERGB(85, 81, 70));
	setfillcolor(EGERGB(85, 81, 70));
	ege_fillcircle(centerX,centerY,radius*1.12);
	
	setcolor(BLACK);
	setfillcolor(BLACK);
	ege_fillcircle(centerX,centerY,radius);
	
	drawScaleNum(centerX,centerY,radius);
	drawPointer(centerX,centerY,radius,angle);
}

int main()
{
	// 初始化图形窗口
	int width = 800, length = 600;
	initgraph(width, length);
	ege_enable_aa(true);
	//仪表背景色
	setrendermode(RENDER_MANUAL);
	setbkcolor(EGERGB(180, 180, 180));
	cleardevice();
	float HYDcenterX = 400;
	float HYDcenterY = 300;
	float HYDradius = 150;
	float HYDangle = 90*PI/180;			//弧度制
	
	while (1) 
	{
		// 清屏
		cleardevice();
		drawHYD(HYDcenterX,HYDcenterY,HYDradius,HYDangle);
		//HYDangle后续需要数据传输实现动态效果
		delay_ms(10);
	}

	getch();
	closegraph();
	return 0;
}